﻿using System;
using NguyenHoa.Web.App_Code;
using NguyenHoa.Services;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;

namespace NguyenHoa.Web.Pages
{
  public partial class ImageDiscovery : BasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      SelectedMenuItemTitle = "";
      if (!IsCallback && !IsPostBack)
      {
        LoadAlbums();
      }

      if (dvList.IsCallback)
      {
        LoadAlbums();
      }
    }

    protected void dvList_DataBound(object sender, EventArgs e)
    {
      if (dvList.DataSource == null) return;
      for (int i = 0; i < dvList.Items.Count; i++)
      {
        DataRow album = ((DataRowView)dvList.Items[i].DataItem).Row;
        HtmlImage img = dvList.FindItemControl("albumImg", dvList.Items[i]) as HtmlImage;
        if (img != null && album != null)
        {
          img.Src = album["FirstPhoto"].ToString();
          img.Attributes.Add("onclick", "doSlideshow('" + album["PhotoList"] + "');return false;");
        }
      }
    }

    private void LoadAlbums()
    {
      DataSet ds = new AlbumService().GetByType(AlbumType.Gallery, VisibleStatus.Active, CurrentLanguage.Id);
      DataTable albumList = null, photoList;
      if (ds != null && ds.Tables.Count > 1)
      {
        albumList = ds.Tables[0];
        photoList = ds.Tables[1];
        if (albumList == null || photoList == null) return;
        foreach (DataRow album in albumList.Rows)
        {
          int id;
          int.TryParse(album["Id"].ToString(), out id);
          DataRow[] photoOfAlbum = photoList.Select("AlbumId=" + id);
          if (photoOfAlbum.Length > 0)
          {
            string firstPhotoPath = string.Empty, nameList = string.Empty;
            foreach (DataRow photo in photoOfAlbum)
            {
              if (!string.IsNullOrEmpty(firstPhotoPath))
              {
                if (photo["Path"] != DBNull.Value && File.Exists(MapPath("../" + photo["Path"])))
                {
                  nameList += Utils.GetFileName("../" + photo["Path"]) + ";";
                }
              }
              else
              {
                if (photo["Path"] != DBNull.Value && File.Exists(MapPath("../" + photo["Path"])))
                {
                  firstPhotoPath = "../" + photo["Path"];
                  nameList += Utils.GetFileName(firstPhotoPath) + ";";
                  album["FirstPhoto"] = firstPhotoPath;
                }
                else
                  firstPhotoPath = string.Empty;
              }
            }

            if (nameList.EndsWith(";"))
            {
              nameList = nameList.Remove(nameList.LastIndexOf(";"));
            }
            if (!string.IsNullOrEmpty(nameList))
            {
              album["PhotoList"] = nameList;
            }
          }
        }
      }

      dvList.DataSource = albumList;
      dvList.DataBind();
    }
  }
}
